package com.hznu.jspFinal.service;

import com.hznu.jspFinal.dao.*;
import com.hznu.jspFinal.entity.Composepaper;
import com.hznu.jspFinal.entity.ExamPaper;
import com.hznu.jspFinal.entity.ExerciseInfo;
import com.hznu.jspFinal.entity.PaperExerTypeInfo;
import com.hznu.jspFinal.myBeans.ExamPaperBean;
import com.hznu.jspFinal.myBeans.Exercise;
import io.swagger.annotations.Example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * author:wp
 * time : 11:28 2018/06/17
 */
@Service
public class ExamPaperServiceImpl implements ExamPaperService {

    @Autowired
    private ExamPaperMapper examPaperMapper;

    @Autowired
    private PaperExerTypeInfoMapper paperExerTypeInfoMapper;

    @Autowired
    private ComposepaperMapper composepaperMapper;

    @Autowired
    private ExerciseInfoMapper exerciseInfoMapper;

    @Autowired
    private TrueFalseMapper trueFalseMapper;

    @Autowired
    private ChoiceMapper choiceMapper;

    @Autowired
    private ShortAnswerMapper shortAnswerMapper;

    @Autowired
    private CompletionMapper completionMapper;


    @Override
    public int add(ExamPaper examPaper) {

        //防止用户设置派生属性
        examPaper.setNoet(0);
        examPaper.setNoe(0);
        examPaper.setTotalpoints(0f);

        examPaperMapper.insertSelective(examPaper);
        return examPaper.getId();
    }

    @Override
    public void addExerTypes(Integer examPaperId,List<PaperExerTypeInfo> paperExerTypeInfos) {

        ExamPaper examPaper = examPaperMapper.selectByPrimaryKey(examPaperId);

        //添加题型数
        int originNoets = 0;
        if(examPaper.getNoet()!=null){originNoets = examPaper.getNoet();}
        examPaper.setNoet(originNoets+paperExerTypeInfos.size());

        //在试卷题型情况表中加入记录
        for(PaperExerTypeInfo paperExerTypeInfo : paperExerTypeInfos){
            paperExerTypeInfo.setExercisenum(0);
            paperExerTypeInfo.setExampaperid(examPaperId);
            paperExerTypeInfoMapper.insertSelective(paperExerTypeInfo);
        }
    }
    @Override
    public void setExerTypes(Integer examPaperId,List<PaperExerTypeInfo> paperExerTypeInfos) {

        ExamPaper examPaper = examPaperMapper.selectByPrimaryKey(examPaperId);

        //设置题型数
        examPaper.setNoet(paperExerTypeInfos.size());

        //删除原来的题型记录
        //暂时不写

        //在试卷题型情况表中加入记录
        for(PaperExerTypeInfo paperExerTypeInfo : paperExerTypeInfos){
            paperExerTypeInfo.setExercisenum(0);
            paperExerTypeInfo.setExampaperid(examPaperId);
            paperExerTypeInfoMapper.insertSelective(paperExerTypeInfo);
        }
    }

    @Override
    public void update(ExamPaper examPaper) {

        ExamPaper oldExampaper = examPaperMapper.selectByPrimaryKey(examPaper.getId());

        //防止用户修改派生量
        examPaper.setTotalpoints(oldExampaper.getTotalpoints());
        examPaper.setNoe(oldExampaper.getNoe());
        examPaper.setNoet(oldExampaper.getNoet());

        examPaperMapper.updateByPrimaryKeySelective(examPaper);
    }

    @Override
    public void delete(Integer id) {
        //组卷表删掉
        composepaperMapper.deleteByPaperid(id);
        //题型表删掉
        paperExerTypeInfoMapper.deleteByPaperid(id);
        //试卷基本信息删掉
        examPaperMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void addExer(Composepaper composepaper) {


        composepaperMapper.insertSelective(composepaper);
        ExamPaper examPaper = examPaperMapper.selectByPrimaryKey(composepaper.getPaperid());

        //更新PaperExerTypeInfo 里的 exercisenum
        List<PaperExerTypeInfo> paperExerTypeInfos = paperExerTypeInfoMapper.selectByExamPaperId(composepaper.getPaperid());
        if(paperExerTypeInfos!=null){
            for(PaperExerTypeInfo paperExerTypeInfo:paperExerTypeInfos){
                ExerciseInfo exerciseInfo = exerciseInfoMapper.selectByPrimaryKey(composepaper.getExerinfoid());
                if(paperExerTypeInfo.getExercisetype() == exerciseInfo.getType())
                {
                    paperExerTypeInfo.setExercisenum(paperExerTypeInfo.getExercisenum()+1);
                    paperExerTypeInfoMapper.updateByPrimaryKeySelective(paperExerTypeInfo);
                    break;
                }
            }
        }

        //更新totalpoints
        examPaper.setTotalpoints(examPaper.getTotalpoints()+composepaper.getPoints());

        //更新题目数noe
        examPaper.setNoe(examPaper.getNoe()+1);

        examPaperMapper.updateByPrimaryKeySelective(examPaper);

    }

    @Override
    public ExamPaperBean findExamPaperDetailsById(Integer examPaperId){
        ExamPaperBean examPaperBean = new ExamPaperBean();

        //设置试卷基本信息
        examPaperBean.setExamPaper(examPaperMapper.selectByPrimaryKey(examPaperId));

        //设置题目类型
        List<PaperExerTypeInfo> paperExerTypeInfos = paperExerTypeInfoMapper.selectByExamPaperId(examPaperId);
        examPaperBean.setPaperExerTypeInfos(paperExerTypeInfos);

        //组装试卷中的题目集
        List<Exercise> exercises = new ArrayList<>();
        List<Composepaper> composepapers = composepaperMapper.selectByPaperId(examPaperId);

        //按照PaperExerTypeInfos的顺序获取题目集
        if(paperExerTypeInfos!=null) {
            for(PaperExerTypeInfo paperExerTypeInfo : paperExerTypeInfos) {
                for (Composepaper composepaper : composepapers) {
                    ExerciseInfo exerciseInfo = exerciseInfoMapper.selectByPrimaryKey(composepaper.getExerinfoid());
                    //组卷表中的这道题如果是当前PaperExerTypeInfo的题型
                    if(exerciseInfo.getType() == paperExerTypeInfo.getExercisetype()){
                        Exercise exercise = new Exercise();
                        exercise.setExerciseInfo(exerciseInfoMapper.selectByPrimaryKey(composepaper.getExerinfoid()));
                        exercise.setPoints(composepaper.getPoints());
                        switch (paperExerTypeInfo.getExercisetype()){
                            case 1:
                                exercise.setChoice(choiceMapper.selectByExerinfoid(exercise.getExerciseInfo().getId()));
                                break;
                            case 2:
                                exercise.setTrueFalse(trueFalseMapper.selectByExerinfoid(exercise.getExerciseInfo().getId()));
                                break;
                            case 3:
                                exercise.setCompletion(completionMapper.selectByExerinfoid(exercise.getExerciseInfo().getId()));
                                break;
                            case 4:
                                exercise.setShortAnswer(shortAnswerMapper.selectByExerinfoid(exercise.getExerciseInfo().getId()));
                                break;
                        }
                        exercises.add(exercise);
                    }
                }
            }
        }

        examPaperBean.setExercises(exercises);

        return examPaperBean;
    }
}
